import { join } from 'path';
export default (api, opt = {}) => {
  const { app, cssPublicPath } = require(join(api.cwd, 'microfe.json'));

  if (app !== 'ant-design-pro') {
    api.config.mountElementId = app;
    api.modifyDefaultConfig(memo => {
      return {
        // 默认使用单数目录
        ...memo,
        cssPublicPath: `${cssPublicPath}/${app}`,
      };
    });
  }

  api.addRendererWrapperWithComponent('@/pages/ErrorWrapper.tsx');

  // api.onPatchRoute(({ route }) => {
  //   route.Routes = ['./src/pages/RouteWrapper.tsx'];
  // });

  api.modifyRoutes(routes => {
    const getRoutes = routes =>
      routes.map(route => {
        if (
          route.path === undefined &&
          (route.component.includes('/404/NotFound.js') || route.component.includes('404.tsx'))
        ) {
          route.component = './src/pages/Base';
        }
        if (route.routes) {
          route.routes = getRoutes(route.routes);
        }
        return route;
      });
    return getRoutes(routes);
  });
};
